package csm.batteryinfo;

import com.mopub.mobileads.MoPubView;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class SettingsDisplayActivity extends Activity {
	public static final String TECHNOLOGY_KEY = "technology";
	public static final String TEMPERATURE_KEY = "temperature";
	public static final String HEALTH_KEY = "health";
	public static final String STATUS_KEY = "status";
	public static final String TEMP_SCALE = "temperature_scale";

	public static final int CELSIUS_SCALE = 0;
	public static final int FAHRENHEIT_SCALE = 1;

	private CheckBox techCheckBox;
	private CheckBox tempCheckBox;
	private CheckBox healthCheckBox;
	private CheckBox statusCheckBox;
	private RadioGroup tempScaleRadioGroup;
	private RadioButton celsiusRadioButton;
	private RadioButton fahrenheitRadioButton;

	private MoPubView mAdView;

	private SharedPreferences settings;
	private SharedPreferences.Editor editor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		settings = Prefs.get(this);
		editor = settings.edit();

		techCheckBox = (CheckBox) findViewById(R.id.techCheckBox);
		tempCheckBox = (CheckBox) findViewById(R.id.tempCheckBox);
		healthCheckBox = (CheckBox) findViewById(R.id.healthCheckBox);
		statusCheckBox = (CheckBox) findViewById(R.id.statusCheckBox);
		tempScaleRadioGroup = (RadioGroup) findViewById(R.id.TempScaleRadioGroup);
		celsiusRadioButton = (RadioButton) findViewById(R.id.CelsiusRadioButton);
		fahrenheitRadioButton = (RadioButton) findViewById(R.id.FahrenheitRadioButton);

		techCheckBox.setChecked(settings.getBoolean(TECHNOLOGY_KEY, true));
		tempCheckBox.setChecked(settings.getBoolean(TEMPERATURE_KEY, true));
		healthCheckBox.setChecked(settings.getBoolean(HEALTH_KEY, true));
		statusCheckBox.setChecked(settings.getBoolean(STATUS_KEY, true));
		if (CELSIUS_SCALE == settings.getInt(TEMP_SCALE, CELSIUS_SCALE)) {
			celsiusRadioButton.setChecked(true);
		} else {
			fahrenheitRadioButton.setChecked(true);
		}

		techCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				editor.putBoolean(TECHNOLOGY_KEY, isChecked);
			}
		});

		tempCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				editor.putBoolean(TEMPERATURE_KEY, isChecked);
			}
		});

		healthCheckBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						editor.putBoolean(HEALTH_KEY, isChecked);
					}
				});

		statusCheckBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						editor.putBoolean(STATUS_KEY, isChecked);
					}
				});

		tempScaleRadioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// write in preferences the scale.
						switch (checkedId) {
						case R.id.CelsiusRadioButton:
							editor.putInt(TEMP_SCALE, CELSIUS_SCALE);
							break;
						case R.id.FahrenheitRadioButton:
							editor.putInt(TEMP_SCALE, FAHRENHEIT_SCALE);
							break;
						}
					}
				});

		// AdMob part
		// try {
		// // Ads section
		// AdView adView = (AdView) findViewById(R.id.adView2);
		// AdRequest ar = new AdRequest();
		// ar.setGender(AdRequest.Gender.MALE);
		// adView.loadAd(ar);
		// } catch (Exception e) {
		// Log.e("BatteryInfo",
		// "There was an excepion when trying to load the Ads", e);
		// e.printStackTrace();
		// }

		try {
			mAdView = (MoPubView) findViewById(R.id.adview);
			// Enter your Ad Unit ID from www.mopub.com
			mAdView.setAdUnitId("agltb3B1Yi1pbmNyDQsSBFNpdGUYwc7TEAw");
			mAdView.loadAd();
		} catch (Throwable t) {
		}
	}

	@Override
	public void onBackPressed() {
		editor.commit();

		super.onBackPressed();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		try {
			mAdView.destroy();
		} catch (Throwable t) {
		}
	}
}